package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.server.event.model.OrderQingqiujieEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderQingqiujieEventListener implements ApplicationListener<OrderQingqiujieEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(OrderQingqiujieEventListener.class);


    @Autowired
    private CompanyService companyService;

    @Override
    @Async
    public void onApplicationEvent(OrderQingqiujieEvent event) {

        Order order = (Order) event.getSource();

        logger.debug("order qingqiujie event:{}", order.getOrderNumber());

        //推送数据到浏览器
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("driverID", order.getDriverId());
        jsonObject.put("type", "qingqiujie");
        Company mainCompany = companyService.getMain();
        WebSocketSend.boardcastToCompany(jsonObject, order.getCompanyId(), mainCompany.getId());
    }

}
